import Axios from 'axios'
import qs from 'qs'
import * as _ from 'lodash'
import imgNull from '@/assets/images/user-space.png'
import SystemConstant from '@/constants/System'

// 在这个文件可以对axios进行一些初始化配置

const ajax = Axios
ajax.fPost = (url, param, config = null) => {
  return Axios.post(url, qs.stringify(param, {indices: false}), config)
}

ajax.jPost = (url, param, config = {headers: {'Content-Type': 'application/json'}}) => {
  return Axios.post(url, JSON.stringify(param), config)
}

function install (Clazz) {
  Clazz.prototype.$ajax = ajax
  Clazz.prototype.$imgUrl = url => _.isEmpty(url) ? imgNull : 'http://' + SystemConstant.DFS_PATH + url
}

/*
 * 使用方法
 * this.$ajax.function()
 * */
export const $ajax = ajax

export default {
  install
}
